package com.zzyl.service.impl.handler;

import com.zzyl.entity.Resource;
import com.zzyl.vo.TreeItemVo;

import java.util.List;
import java.util.stream.Collectors;

public class ThirdResourceFilterHandler implements ResourceFilterHandler{
    private ResourceFilterHandler resourceFilterHandler;

    public void setNextHandler(ResourceFilterHandler resourceFilterHandler) {
        this.resourceFilterHandler = resourceFilterHandler;
    }

    @Override
    public void filter(List<Resource> list, TreeItemVo root) {
        root.getChildren().stream().map(r ->{
            r.getChildren().stream().map(m->{
                List<TreeItemVo> thrid = list.stream().filter(n -> {
                    if (n.getParentResourceNo().equals(m.getId())) {
                        return true;
                    } else {
                        return false;
                    }
                }).map(n -> {
                    TreeItemVo vo = new TreeItemVo();
                    vo.setId(n.getResourceNo());
                    vo.setLabel(n.getResourceName());
                    return vo;
                }).collect(Collectors.toList());
                m.setChildren(thrid);
                return m;
            }).collect(Collectors.toList());
            return r;
        }).collect(Collectors.toList());
        if(null != resourceFilterHandler){
            resourceFilterHandler.filter(list,root);
        }
    }
}
